/**
 * Copyright 2020 OPSLI 快速开发平台 https://www.opsli.com
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opsli.modulars.business.projectTask.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.opsli.core.utils.UserUtil;
import org.opsli.modulars.business.projectTask.entity.ProjectTask;
import org.opsli.modulars.business.projectTask.entity.ProjectTaskData;
import org.opsli.modulars.business.projectTask.mapper.ProjectTaskMapper;
import org.opsli.modulars.business.projectTask.service.IProjectTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.opsli.core.base.service.impl.CrudServiceImpl;

import org.opsli.api.wrapper.projectTask.ProjectTaskModel;

import java.util.ArrayList;
import java.util.List;


/**
 * 工程任务 Service Impl
 *
 * @author Ljc
 * @date 2022-04-25 09:54:35
 */
@Service
public class ProjectTaskServiceImpl extends CrudServiceImpl<ProjectTaskMapper, ProjectTask, ProjectTaskModel>
    implements IProjectTaskService {

    @Autowired(required = false)
    private ProjectTaskMapper mapper;

    @Override
    public Integer allocationUserAdd(String userId,ProjectTaskModel projectTaskModel) {

        return null;
    }

    @Override
    public Page<ProjectTaskData> getProjectTaskList(Page<ProjectTaskData> page, ProjectTaskData projectTask, Integer beginSize, Integer endSize) {

        String userNameId = projectTask.getUserNameId();

        List<ProjectTaskData> pList=mapper.getUserTaskList(projectTask);
        List<ProjectTaskData> list=new ArrayList<>();

        for (ProjectTaskData task:pList){
            String idName=task.getUserId();
            String[] arrayName=idName.split(",");
            for (int i=0;i<arrayName.length;i++){
                String userIdName=arrayName[i];
                if(userNameId.equals(userIdName)){

                    String userName=null;
                    for (int f=0;f<arrayName.length;f++){
                        String  userUtilName = UserUtil.getUserName(arrayName[f]);
                        if(f==0){
                            userName=userUtilName;
                        }else {
                            userName=userName+","+userUtilName;
                        }

                    }
                    task.setUserId(userName);
                    task.setUserNameId(userNameId);

                    list.add(task);
                }
            }
        }
        return page.setRecords(list);
    }
}